using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Alicanto.Api
{
    public class IntIter : IEnumerable<int>
    {
        private int _from;
        private int _to;

        public IntIter(int from, int to)
        {
            _from = from;
            _to = to;
        }

        public IEnumerator<int> GetEnumerator()
        {
            return new Enumerator(_from, _to);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new Enumerator(_from, _to);
        }

        class Enumerator : IEnumerator<int>
        {
            private int _from;
            private int _to;

            private int _current;
            private bool _init;

            public Enumerator(int from, int to)
            {
                _from = from;
                _to = to;

                _init = true;
            }

            public int Current
            {
                get
                {
                    return _current;
                }
            }

            public void Dispose()
            {

            }

            object System.Collections.IEnumerator.Current
            {
                get
                {
                    return _current;
                }
            }

            public bool MoveNext()
            {
                if (_init)
                {
                    _init = false;
                    _current = _from;
                }
                else
                {
                    // +
                    if (_to - _from > 0)
                        _current++;
                    // -
                    else
                        _current--;
                }

                return _to - _current != 0;
            }

            public void Reset()
            {
                _init = true;
            }
        }

    }
}
